package com.study.digui;

import java.util.Arrays;
import java.util.List;

/**
 * 递归求和
 *
 * @author yyb8152
 * @since 20220820
 **/
public class DiguiSum {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 4, 6, 10, 300);
        //System.out.println(getSum(list));
        // 递归递减
        // decrease(8);
        // 递归相乘
        // System.out.println(multi(4));
        // 递归求列表的元素个数
        // System.out.println(count(list));
        // 求列表中最大的数
        System.out.println(maxNum(list));

    }

    /**
     * 递归求和
     *
     * @param list
     * @return
     */
    public static Integer getSum(List<Integer> list) {
        if (list.size() == 0) {
            return 0;
        }
        return list.get(0) + getSum(list.subList(1, list.size()));
    }

    /**
     * 递归递减
     */
    public static Integer decrease(Integer num) {
        System.out.println(num);
        // 基线条件
        if (num < 1) {
            return num;
        }

        // 递归条件
        return decrease(num - 1);

    }

    /**
     * 递归相乘
     */
    public static Integer multi(Integer num) {
        if (num == 1 || num == 0) {
            return num;
        }
        return num * multi(num - 1);
    }

    /**
     * 递归求列表的元素个数
     */
    public static Integer count(List<Integer> numList) {
        if (numList.isEmpty()) {
            return 0;
        }
        return 1 + count(numList.subList(1, numList.size()));
    }

    /**
     * 递归-求列表最大的数
     */
    public static Integer maxNum(List<Integer> numList) {
        if (numList.size() == 2) {
            return numList.get(0) >= numList.get(1) ? numList.get(0) : numList.get(1);
        }
        Integer subMax = maxNum(numList.subList(1, numList.size()));

        return numList.get(0) >= subMax ? numList.get(0) : subMax;


    }

}
